/**
 * 
 */
package cn.sundat.web.action;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.sundat.model.FriendLink;
import cn.sundat.model.SysCfg;
import cn.sundat.service.FriendLinkService;

/**
 * @author XIN
 *
 */
public class LinkAction extends ActionSupport implements ModelDriven<FriendLink> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5339442512525809002L;

	@Resource(name = "friendLinkService")
	private FriendLinkService friendLinkService;

	private FriendLink friendLink = new FriendLink();

	public FriendLink getModel() {
		// TODO Auto-generated method stub
		return friendLink;
	}

	public String add() throws Exception {
		friendLinkService.addFriendLink(friendLink);
		return NONE;
	}

	public String findAllFriendLink() throws Exception {
		List<FriendLink> findFriendLinkByPage = friendLinkService.findAllFriendLink();
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(JSONObject.valueToString(findFriendLinkByPage));
		writer.flush();
		writer.close();
		return NONE;
	}

	public String findFriendLinkCount() throws Exception {
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(friendLinkService.findCount());
		writer.flush();
		writer.close();
		return NONE;
	}

	public String findFriendLinkBykey() throws Exception {
		String keyWords = ServletActionContext.getRequest().getParameter("keywords");
		List<FriendLink> findFriendLinkByKey = friendLinkService.findFriendLinkBykey(keyWords);
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(JSONObject.valueToString(findFriendLinkByKey));
		writer.flush();
		writer.close();
		return NONE;
	}

	public String delBatch() throws Exception {
		String batchList = ServletActionContext.getRequest().getParameter("batchList");
		boolean result = friendLinkService.delBatch(batchList);
		System.out.println(result);
		return NONE;
	}

	public String editFriendList() throws Exception {
		friendLinkService.editFriendLink(friendLink);
		return NONE;

	}

	public String findFriendLinkById() throws Exception {
		String id = ServletActionContext.getRequest().getParameter("id");
		ActionContext.getContext().getValueStack().set("findFriendLink",
				friendLinkService.findFriendLinkById(Integer.valueOf(id)).get(0));
		return "linkEdit";
	}

	public String sysInfoList() throws Exception {
		ActionContext.getContext().getValueStack().set("sysInfoList", friendLinkService.findAllInfo());
		return "sysInfoList";
	}

	public String editSysInfo() throws Exception {
		for (int i = 1; i < 8; i++) {
			String id = ServletActionContext.getRequest().getParameter("id" + i);
			String name = ServletActionContext.getRequest().getParameter("name" + i);
			String value = ServletActionContext.getRequest().getParameter("value" + i);
			String note = ServletActionContext.getRequest().getParameter("note" + i);
			SysCfg sysCfg = new SysCfg(Integer.valueOf(i), name, value, note);
			friendLinkService.editSysInfo(sysCfg);
		}
		return NONE;
	}
	public String sysInfoListAjax()throws Exception {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		writer.print(JSONObject.valueToString(friendLinkService.findAllInfo()));
		writer.flush();
		writer.close();
		return NONE;
	}
}
